package cn.itbaizhan.tyut.exam.sys.servlets;


import cn.itbaizhan.tyut.exam.common.PageControl;
import cn.itbaizhan.tyut.exam.common.Pager;
import cn.itbaizhan.tyut.exam.common.Tools;
import cn.itbaizhan.tyut.exam.model.Subject;
import cn.itbaizhan.tyut.exam.sys.services.impl.SubjectService;
import cn.itbaizhan.tyut.exam.sys.services.interfaces.ISubjectService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class SubjectServlet extends HttpServlet {

    ISubjectService service = new SubjectService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String cmd = request.getParameter("cmd");
        if (cmd.equals("add")) {
            addsubject(request, response);
        } else if (cmd.equals("list")) {
            list(request, response);
        } else if (cmd.equals("toedit")) {
            toedit(request, response);
        } else if (cmd.equals("edit")) {
            edit(request, response);
        }
    }

    /**
     * 修改试题功能
     *
     * @param request
     * @param response
     */
    private void edit(HttpServletRequest request, HttpServletResponse response) {

        Subject subject = new Subject();

        try {
            BeanUtils.populate(subject, request.getParameterMap());
            Integer rtn = service.edit(subject);
            if (rtn > 0) {
                response.sendRedirect(Tools.Basepath(request, response) + "sys/subject?cmd=list");
            } else {
                request.setAttribute("msg", "编辑试题功能失败！");
                request.getRequestDispatcher("/sys/subject/edit.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化修改页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void toedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Subject subject = new Subject();
        subject.setSid(Integer.parseInt(request.getParameter("id")));
        subject = service.detail(subject);
        if (subject != null) {
            request.setAttribute("item", subject);
            request.getRequestDispatcher("/sys/subject/edit.jsp").forward(request, response);
        } else {
            request.setAttribute("msg", "需要修改的试题功能不存在。");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }

    /**
     * 查询试题列表
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void list(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

        String scontent = request.getParameter("scontent");
        Subject subject = new Subject();
        if (scontent != null && !scontent.equals("")) {
            subject.setScontent(scontent);
        }

        PageControl pc = new PageControl();
        Integer currindex = 1;
        if (request.getParameter("index") != null) {
            currindex = Integer.parseInt(request.getParameter("index"));
        }
        pc.setCurrentindex(currindex);
        //pc.setPagesize(5);

        Pager<Subject> pager = service.list(subject, pc);
        request.setAttribute("pager", pager);
        request.getRequestDispatcher("/sys/subject/list.jsp").forward(request, response);
    }

    /**
     * 增加试题功能
     *
     * @param request
     * @param response
     */
    private void addsubject(HttpServletRequest request, HttpServletResponse response) {

        try {
            String scontent = request.getParameter("scontent");
            if (scontent == null || scontent.equals("")) {
                request.setAttribute("msg", "题干不能为空");
                request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
                return;
            }
            String sa = request.getParameter("sa");

            String type = request.getParameter("type");
            String sb = request.getParameter("sb");
            String sc = request.getParameter("sc");
            String sd = request.getParameter("sd");;
            String skey = request.getParameter("skey");
            String answer = request.getParameter("answer");
            if ("0".equals(type)) { // 选择题
                if (sa == null || sa.equals("")) {
                    request.setAttribute("msg", "选项A不能为空");
                    request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
                    return;
                }
                if (sb == null || sb.equals("")) {
                    request.setAttribute("msg", "选项B不能为空");
                    request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
                    return;
                }
                if (sc== null || sc.equals("")) {
                    request.setAttribute("msg", "选项C不能为空");
                    request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
                    return;
                }
                if (sd == null || sd.equals("")) {
                    request.setAttribute("msg", "选项D不能为空");
                    request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
                    return;
                }
            } else if ("1".equals(type)) { // 判断题
            }

            if (skey == null || skey.equals("")) {
                request.setAttribute("msg", "答案不能为空");
                request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
                return;
            }

            Subject subject = new Subject();
            BeanUtils.populate(subject, request.getParameterMap());
            Integer rtn = service.addsubject(subject);
            if (rtn > 0) {
                response.sendRedirect(Tools.Basepath(request, response) + "sys/subject?cmd=list");
            } else {
                request.setAttribute("msg", "增加试题功能失败或请不要添加相同试题！");
                request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
    }


}
